﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class FormDeliver
    Dim objCA As New clsCPCK
    Public _sys_id As String
    Dim objProdInfo As New clsProdInfo
    Dim objPgdMes As New clsPgdMes
    Public usr As String
    Dim warehousecode As String '仓库编码
    Dim httpHelper As New HttpHelper
    Private Sub FormDeliver_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        warehousecode = GetIniFile("WAREHOUSE", "warehouse_code", "", g_szInitFileName)
        txtProdId.Focus()
    End Sub

    Private Sub txtProdId_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtProdId.KeyPress
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) = False Then
            If e.KeyChar = vbCr Then
                Dim dt As DataTable = objCA.getListByProdId(txtProdId.Text.Trim(), "0")
                If dt.Rows.Count = 1 Then
                    _sys_id = dt.Rows(0)("sys_id")
                    rkdt.Text = dt.Rows(0)("into_time")
                    loc.SelectedText = dt.Rows(0)("wh_no")
                    If dt.Rows(0)("hour_length").Equals(DBNull.Value) Then
                        txt_hour_length.Text = ""
                    Else
                        txt_hour_length.Text = dt.Rows(0)("hour_length")
                    End If
                Else
                    If dt.Rows.Count = 0 Then
                        MsgBox("未查询到此生产编号库存信息")
                        txtProdId.Text = String.Empty
                        txtProdId.Focus()
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub txtProdId_TextChanged(sender As Object, e As EventArgs) Handles txtProdId.TextChanged
        clear()
    End Sub
    Public Sub clear()
        rkdt.Text = String.Empty
        loc.Text = String.Empty
        _sys_id = String.Empty
    End Sub

    Private Sub btnOUT_Click(sender As Object, e As EventArgs) Handles btnOUT.Click
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) Then Exit Sub
        If String.IsNullOrEmpty(txt_hour_length.Text) Then
            MsgBox("未获取到存放时长")
            Exit Sub
        End If
        If String.IsNullOrEmpty(_sys_id) = False Then
            If String.IsNullOrEmpty(txt_hour_length.Text) = False Then
                If Integer.Parse(txt_hour_length.Text.Trim()) < 8 Then
                    MsgBox("存放时长 < 8小时")
                    Exit Sub
                End If
                Dim dtxt As DataTable = objProdInfo.getProdInfoById(txtProdId.Text.Trim(), "3")
                If dtxt.Rows.Count > 0 Then
                    If objCA.outCK(loc.Text, usr, _sys_id, txtProdId.Text.Trim()) = -2 Then
                        MsgBox("出库失败")
                    Else
                        Dim dttp As DataTable = objProdInfo.selectProdInfo(txtProdId.Text.Trim)
                        Dim dtssd As DataTable = objPgdMes.getList(dttp.Rows(0)("b_pgd_yzz_details_sys_id").ToString())
                        Dim b_gtPgdMesTypeValue As String = dtssd.Rows(0)("type_value").ToString()
                        Dim rnt_int As Integer = mesCk(usr, warehousecode, b_gtPgdMesTypeValue)
                        clear()
                        If rnt_int = 1 Then
                            MsgBox("出库成功")
                        Else
                            MsgBox("出库成功,MES出库失败")
                        End If
                        txtProdId.Text = String.Empty
                        txtProdId.Focus()
                    End If
                Else
                    MsgBox("未查询到入库状态的此生产编号信息")
                End If
            Else
                MsgBox("未获取到存放时长！")
            End If
        End If
    End Sub

    Private Function mesCk(oper_num As String, warehousecode As String, productorname As String) As Integer
        Dim rtn_int As Integer = 0
        Dim params As String = "{"
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """xhcount"":""" & Double.Parse(oper_num) & """," '出库数量
        params &= """warehousecode"":""" & warehousecode & """," '出库仓库编码
        params &= """productorname"":""" & productorname & """" '产品编码
        params &= "}"
        Dim rtn As String = HttpHelper.HttpPostWebService("materialsConsumption", params)
        Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
        Dim code As String = jo("code").ToString() '输出0
        Dim msg As String = jo("msg").ToString() '输出操作成功
        If code.Equals("0") And msg.Equals("操作成功") Then
            rtn_int = 1
        End If
        Return rtn_int
    End Function

End Class